<template>
  <div>
    <div class="header_search_nav search_form">
      <div class="ipt_left">
        <el-form ref="queryForm" :model="queryParams" label-width="80px">
          <el-row>
            <el-col :span="6">
              <el-form-item label="商品名称" prop="name">
                <el-input
                  class="w-200"
                  v-model="queryParams.name"
                  placeholder="请输入商品名称"
                  @keyup.enter.native="handleQuery"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="商品分类" prop="productTypeId">
                <el-select
                  class="w-200"
                  v-model="queryParams.productTypeId"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in goodSortList"
                    :key="item.typeId"
                    :label="item.typeName"
                    :value="item.typeId"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="商品状态" prop="sellStatus">
                <el-select
                  class="w-200"
                  v-model="queryParams.sellStatus"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in SellStatusOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>

      <div class="btn_right">
        <button class="btn1" @click="handleQuery">筛选</button>
        <button class="btn2" @click="resetQuery">重置</button>
      </div>
    </div>

    <!-- 商品列表 -->
    <div class="table_card">
      <!-- 上部分 title -->
      <div class="table_card_top">
        <span class="solid_left"></span>
        <div class="table_card_title">商品列表</div>
        <div class="op_right">
          <button class="primary_button" @click="publishGood">发布商品</button>
        </div>
      </div>

      <div class="table_list">
        <el-table :data="goodList" stripe style="width: 100%">
          <el-table-column prop="reorder" label="商品排序" width="300px">
          </el-table-column>
          <el-table-column prop="name" label="商品名称"> </el-table-column>
          <el-table-column prop="productTypeName" label="商品分类">
          </el-table-column>
          <el-table-column prop="price" label="商品金额（元）">
          </el-table-column>
          <el-table-column prop="sellStatus" label="商品状态">
            <template slot-scope="scope">
              {{ sellStatusObj[scope.row.sellStatus] }}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="300px">
            <template slot-scope="scope">
              <el-button size="mini" type="text" @click="Edit(scope.row)"
                >编辑</el-button
              >
              <el-button
                size="mini"
                type="text"
                @click="Takedown(scope.row, 0)"
                :disabled="
                  scope.row.sellStatus === 0 || scope.row.sellStatus === '0'
                    ? true
                    : false
                "
              >
                <span class="separate_left">|</span> 上架<span
                  class="separate_right"
                  >|</span
                >
              </el-button>
              <el-button
                size="mini"
                type="text"
                @click="Takedown(scope.row, 1)"
                :disabled="
                  scope.row.sellStatus === 1 || scope.row.sellStatus === '1'
                    ? true
                    : false
                "
              >
                <span class="separate_left"></span> 下架<span
                  class="separate_right"
                ></span>
              </el-button>
              <el-button size="mini" type="text" @click="DelSort(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <pagination
          :total="total"
          :page.sync="queryParams.pageNo"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>

      <!-- 新建商品 -->
      <el-dialog
        :title="title"
        :visible.sync="newGooddialogVisible"
        width="500px"
        :before-close="CancelGoodSubmit"
        class="no-dialog-padding"
      >
        <span>
          <div class="goodForm">
            <el-form ref="goodForm" :rules="goodRules" :model="goodForm">
              <el-row>
                <el-col>
                  <el-form-item label="商品名称" prop="name">
                    <el-input
                      class="w-100"
                      v-model="goodForm.name"
                      placeholder="请输入商品名称"
                    ></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-form-item label="商品排序" prop="reorder">
                  <el-input-number
                    class="w-100 text-left"
                    placeholder="请输入排序号"
                    v-model="goodForm.reorder"
                    :min="0"
                    :step="1"
                    :precision="0"
                    :controls="false"
                  ></el-input-number>
                </el-form-item>
              </el-row>
              <el-row>
                <el-form-item label="商品分类" prop="productTypeId">
                  <el-select
                    v-model="goodForm.productTypeId"
                    placeholder="请选择项目分类"
                    class="w-100"
                  >
                    <el-option
                      v-for="item in goodSortList"
                      :key="item.typeId"
                      :label="item.typeName"
                      :value="item.typeId"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-row>
              <el-row>
                <el-form-item label="商品价格（元）" prop="price">
                  <el-input-number
                    v-model="goodForm.price"
                    placeholder="请输入商品价格"
                    :precision="2"
                    :min="0"
                    :controls="false"
                    class="w-100 text-left"
                  ></el-input-number>
                </el-form-item>
              </el-row>
            </el-form>
          </div>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button class="cancel_btn" @click="CancelGoodSubmit"
            >取 消</el-button
          >
          <el-button type="primary" @click="handleGoodSubmit">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  GetStoresGoodPage,
  CreateStoresGood,
  UpdateStoresGood,
  delStoresGood,
  getGoodAllSort,
  updateUpDown,
} from "@/api/business/good";
import { getStoreId } from "@/utils/auth";
export default {
  name: "EryuoaStoreGood",

  data() {
    return {
      newGooddialogVisible: false, // 新建商品
      title: "",
      goodForm: {
        name: "",
        reorder: "",
        price: "",
        productTypeId: "", // 分类id
        storeId: "",
      },
      value1: "",
      goodList: [],
      goodSortList: [],

      SellStatusOptions: [
        {
          value: "",
          label: "全部",
        },
        {
          value: "0",
          label: "上架",
        },
        {
          value: "1",
          label: "下架",
        },
      ],

      sellStatusObj: {
        0: "上架",
        1: "下架",
      },

      goodRules: {
        name: [{ required: true, message: "请输入商品名称", trigger: "blur" }],
        reorder: [{ required: true, message: "请输入排序号", trigger: "blur" }],
        productTypeId: [
          { required: true, message: "请选择项目分类", trigger: "blur" },
        ],
        price: [{ required: true, message: "请输入项目价格", trigger: "blur" }],
      },

      value: "",
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        name: "",
        productTypeId: "",
        sellStatus: null,
      },
      total: 0,
    };
  },

  mounted() {
    this.getList();
    this.getSortList();
  },

  methods: {
    // 商品分类
    getSortList() {
      this.loading = true;
      getGoodAllSort({})
        .then((res) => {
          console.log(res);
          const { code, data } = res;
          if (code === 0) {
            this.goodSortList = data;
          }
        })
        .catch((err) => {});
    },

    CancelGoodSubmit() {
      this.resetFormData("goodForm");
      this.goodForm.productId = "";
      this.$nextTick(() => {
        this.newGooddialogVisible = false;
      });
    },

    // 表单重置
    resetFormData(formName) {
      console.log(this.$refs[formName]);
      this.$refs[formName].resetFields();
    },

    // 筛选
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetFormData("queryForm");
      this.handleQuery();
    },

    resQ() {
      this.queryParams.pageNo = 1;
      this.total = 0;
      this.goodList = [];
      this.getList();
      this.getSortList();
    },

    // 商品列表
    getList() {
      this.loading = true;
      GetStoresGoodPage(this.queryParams)
        .then((res) => {
          console.log(res);
          this.loading = false;
          const { code, data } = res;
          if (code === 0) {
            this.goodList = data.list;
            this.total = data.total;
          }
        })
        .catch((err) => {
          this.loading = false;
        });
    },

    publishGood() {
      this.newGooddialogVisible = true;
      this.title = "新建商品";
      this.getSortList();
    },

    // 新建商品 弹框 确认
    handleGoodSubmit() {
      this.$refs["goodForm"].validate((valid) => {
        if (valid) {
          if (!this.goodForm.productId) {
            this.Creategood();
          } else {
            this.Updategood();
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    // 新建商品
    Creategood() {
      let goodItem = JSON.parse(JSON.stringify(this.goodForm));
      goodItem.storeId = getStoreId();
      CreateStoresGood(goodItem)
        .then((res) => {
          const { code } = res;
          if (code === 0) {
            this.getList();
            this.$modal.msgSuccess("操作成功");
            this.CancelGoodSubmit();
          }
        })
        .catch((err) => {});
    },

    // 编辑商品
    Updategood() {
      const { name, price, reorder, productId, productTypeId, storeId } =
        this.goodForm;
      UpdateStoresGood({
        name: name,
        price: price,
        reorder: reorder,
        productId: productId,
        productTypeId: productTypeId,
        storeId: storeId,
      })
        .then((res) => {
          const { code } = res;
          if (code === 0) {
            this.getList();
            this.$modal.msgSuccess("操作成功");
            this.CancelGoodSubmit();
          }
        })
        .catch((err) => {});
    },

    // 编辑商品
    Edit(row) {
      this.newGooddialogVisible = true;
      this.title = "编辑商品";
      this.$nextTick(() => {
        const item = JSON.parse(JSON.stringify(row));
        this.goodForm.name = item.name;
        this.goodForm.reorder = item.reorder;
        this.goodForm.productTypeId = item.productTypeId;
        this.goodForm.price = item.price;
        this.goodForm.productId = item.productId;
        this.goodForm.storeId = item.storeId;
      });
    },

    // 商品列表 删除
    DelSort(row) {
      this.DeleteSort(row);
    },

    DeleteSort(row) {
      let productId = row.productId;
      this.$confirm("商品删除后无法撤销, 是否删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delStoresGood({ productId: productId })
            .then((res) => {
              const { code } = res;
              if (code === 0) {
                this.getList();
                this.$message({
                  type: "success",
                  message: "删除成功!",
                });
              }
            })
            .catch(() => {});
        })
        .catch(() => {});
    },

    // 列表 下架
    Takedown(row, sellStatus) {
      this.$confirm(
        "商品下架后门店无法使用, 是否" +
          (sellStatus === 0 ? "上架" : "下架") +
          "?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          updateUpDown({
            productId: row.productId,
            sellStatus: sellStatus,
          }).then((res) => {
            const { code } = res;
            if (code === 0) {
              this.getList();
              this.$message({
                type: "success",
                message: "操作成功!",
              });
            }
          });
        })
        .catch(() => {});
    },
  },
};
</script>

<style lang="scss" scoped>
.goodTop {
  width: 100%;
  height: 48px;
  background: #ffffff;
  display: flex;
  font-size: 14px;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.6);
  line-height: 48px;
  padding-left: 16px;
  .good_list {
    margin-right: 32px;
  }

  .good_active {
    color: #0052d9;
    border-bottom: 1px solid #0052d9;
  }
}

.w-200 {
  width: 200px;
  background: #ffffff;
  border-radius: 4px;
}

.client_name {
  font-size: 14px;
  font-weight: 400;
  color: #0052d9;
}

.give {
  height: 40px;
  font-size: 14px;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.85);
  margin-top: 40px;
  line-height: 40px;
}

.el-form-item {
  margin-bottom: 20px;
}

.recharge_form {
  padding: 10px 24px;
}

.memberName {
  font-size: 14px;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.85);
}
.el_row_item {
  margin-bottom: 10px;
}
.el_row_date {
  margin-top: 20px;
}
.el_row_reason {
  margin-top: 10px;
}

.el_row_date {
  .el-form-item {
    margin-bottom: 0px;
  }
}

.separate_left {
  width: 1px;
  height: 14px;
  // background: #e9e9e9;
  margin-right: 12px;
}
.separate_right {
  width: 1px;
  height: 14px;
  // background: #e9e9e9;
  margin-left: 12px;
}
.goodForm {
  padding: 0 24px;
}
</style>
